﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfControlLibrary.GridItems
{
    /// <summary>
    /// Grid.xaml 的交互逻辑
    /// </summary>
    public partial class Grid : UserControl
    {
        public Grid()
        {
            InitializeComponent();
            this.SizeChanged += Grid_SizeChanged;
        }

        private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Fresh();
        }

        public List<GridItem> DataSource
        {
            get { return (List<GridItem>)GetValue(DataSourceProperty); }
            set
            {
                SetValue(DataSourceProperty, value);
                Fresh();
            }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSource", typeof(List<GridItem>), typeof(Grid), new PropertyMetadata(null));



        


        /// <summary>
        /// 在这里做格式化操作，把自制的 GridItem 分布到当前页面的长宽之中。
        /// </summary>
        public void Fresh()
        {
            if (DataSource != null)
            {
                var GridWidth = this.ActualWidth;
                var GridHight = this.ActualHeight;


                double Blank = 16;
                double HalfBlank = 8;

                double ItemWidth = 200;
                double ItemHeight = 100;


                this.Grid_Fill_Content.Children.Clear();
                for (int i = 0; i < DataSource.Count; i++)
                {
                    GridCalculate(i, GridWidth, Blank, HalfBlank, ItemWidth, ItemHeight);
                }
                for (int i = 0; i < DataSource.Count; i++)
                {
                    var item = new WpfControlLibrary.GridItems.Item();
                    item.DataSource = DataSource[i];

                    item.ItemMargin = ItemMarginCalculate(i, GridWidth, Blank, HalfBlank, ItemWidth, ItemHeight);
                    //item.ItemMargin = "0 0 0 0";
                    this.Grid_Fill_Content.Children.Add(item);

                }
            }
            
        }

        /// <summary>
        /// 计算一下Grid的高度
        /// </summary>
        /// <param name="index"></param>
        /// <param name="GridWidth"></param>
        /// <param name="Blank"></param>
        /// <param name="HalfBlank"></param>
        /// <param name="ItemWidth"></param>
        /// <param name="ItemHeight"></param>
        private void GridCalculate(int index, double GridWidth, double Blank, double HalfBlank, double ItemWidth, double ItemHeight)
        {

            double X = Blank;
            double Y = Blank + ItemHeight;

            this.Grid_Fill_Content.Height = ItemHeight + Blank + Blank;
            for (int i = 0; i <= index; i++)
            {
                if (i == 0)
                {
                    //不管
                }
                else
                {
                    X = X + ItemWidth + HalfBlank;
                }

                if (X != Blank)
                {
                    if ((X + ItemWidth + Blank) > GridWidth)
                    {
                        X = Blank;
                        Y = Y + HalfBlank + ItemHeight;

                        this.Grid_Fill_Content.Height = this.Grid_Fill_Content.Height + HalfBlank + ItemHeight;
                    }
                }

            }



        }

        /// <summary>
        /// 位置计算器
        /// </summary>
        /// <param name="GridWidth">外壳宽度</param>
        /// <param name="Blank">留白区与尺寸</param>
        /// <param name="HalfBlank">留白尺寸一半</param>
        /// <param name="ItemWidth"></param>
        /// <param name="ItemHeight"></param>
        /// <returns></returns>
        private string ItemMarginCalculate(int index,double GridWidth,double Blank,double HalfBlank,double ItemWidth,double ItemHeight)
        {
            string MarginResult = "0";

            double X = Blank;
            double Y = Blank + this.Grid_Fill_Content.Height / 2 * -1 + ItemHeight/2;

            for (int i = 0; i <= index; i++)
            {
                if (i == 0)
                {
                    //不管
                }
                else
                {
                    X = X + ItemWidth + HalfBlank;
                }

                if (X != Blank)
                {
                    if ((X + ItemWidth + Blank) > GridWidth)
                    {
                        X = Blank;
                        Y = Y + HalfBlank + ItemHeight;

                    }
                }
                    
            }



            MarginResult = $"{X} {Y} {X * -1} {Y * -1}";

            return MarginResult;
        }
    }
}
